package com.manor.paradise.fragment

import android.content.Intent
import android.net.Uri
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.manor.common.fragment.BaseFragment
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.ClickUtil
import com.manor.common.utils.GsonUtil
import com.manor.common.utils.KLog
import com.manor.common.utils.ToastUtil
import com.manor.paradise.R
import com.manor.paradise.activity.ItInviteActivity
import com.manor.paradise.adapter.ReikIndexAdapter
import com.manor.paradise.bean.ReikindexBean
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import com.yanzhenjie.permission.Action
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission
import kotlinx.android.synthetic.main.fragment_sub_reik.*

/**
 * 灵气指数
 */
class ReikiSubFragment :BaseFragment() {
    internal lateinit var mAdapter: ReikIndexAdapter
    private var isHire = false
    private var isAuth = false
    private var isAchieve = false
    private var pageNum = 1
    private val mDataList = mutableListOf<ReikindexBean.RecordsBean>()

    companion object{
        fun newInstance(isHire: Boolean,isAchieve: Boolean,isAuth: Boolean): ReikiSubFragment{
            val args = bundleOf(
                    "isHire" to isHire,
                    "isAchieve" to isAchieve,
                    "isAuth" to isAuth)

            val fragment = ReikiSubFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun getLayoutRes(): Int = R.layout.fragment_sub_reik

    override fun initView() {
        isHire = arguments?.getBoolean("isHire") ?: false
        isAuth = arguments?.getBoolean("isAuth") ?: false
        isAchieve = arguments?.getBoolean("isAchieve") ?: false
        mRecycler.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
        mAdapter = ReikIndexAdapter(R.layout.item_reiki_index_new, mDataList) //item_reiki_index
        mRecycler.adapter = mAdapter
        mAdapter.addChildClickViewIds(R.id.ll_tel)
        mAdapter.setOnItemChildClickListener(itemChildClickListener)
        mAdapter.setOnItemClickListener(itemClickListener)
        refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageNum++
                setData(false)
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageNum = 1
                setData(true)
            }
        })
        setData(true)
    }

    fun setData(isRefresh :Boolean) {
        ElvesHttpUtil.getAnimaInfoNew(pageNum, isHire, isAchieve, isAuth, object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    onError()
                    return
                }
                var reikindexBean: ReikindexBean = GsonUtil.gsonToBean<ReikindexBean>(info, ReikindexBean::class.java)
                KLog.e("json", reikindexBean)
                if (pageNum == 1) {
                    mDataList.clear()
                    refreshLayout?.finishRefresh()
                }
                mDataList.addAll(reikindexBean.records)
                if (mDataList == null || mDataList.isEmpty()) {
                    mAdapter?.setEmptyView(R.layout.view_empty_default)
                }
                mAdapter.notifyDataSetChanged()
                refreshLayout?.finishLoadMore()

            }


            override fun onError() {
                super.onError()
                if (!isRefresh) {
                    pageNum--
                    refreshLayout.finishLoadMore()
                }
            }


        })
    }

    private val itemChildClickListener = OnItemChildClickListener { adapter, view, position ->
        val id = view.id
        if (id == R.id.ll_tel) {
            callPhone(mAdapter.getItem(position).mobile)
        }
    }

    private val itemClickListener = OnItemClickListener {
        _, _, position ->
        if (ClickUtil.canClick()){
            activity?.let { ItInviteActivity.forward(it, mDataList[position]) }
        }
    }

    private fun callPhone(phoneNum: String) {
        AndPermission.with(this)
                .runtime()
                .permission(Permission.CALL_PHONE)
                .onGranted {
                    val intent = Intent(Intent.ACTION_DIAL)
                    val data = Uri.parse("tel:$phoneNum")
                    intent.data = data
                    startActivity(intent)
                }
                .onDenied(Action { data ->

                })
                .start()

    }

    override fun onDestroy() {
        activity?.let { Glide.get(it).clearMemory() }
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_GET_INFO)
        super.onDestroy()
    }


}